package com.yc.book.util;

import com.yc.book.biz.BookBiz;
import com.yc.book.dao.BookDao;
import com.yc.common.bean.Result;
import com.yc.common.biz.BizException;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

import java.io.IOException;

@WebServlet(name = "SaveBookServlet", value = "/saveBook.s")
public class SaveBookServlet extends HttpServlet {
    private BookBiz biz = new BookBiz();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        String id = request.getParameter("id");
        String bookname = request.getParameter("bookname");
        String bookpress = request.getParameter("bookpress");
        String pressdate = request.getParameter("pressdate");
        String bookauthor = request.getParameter("bookauthor");
        String bookcount = request.getParameter("bookcount");
        String bookimage = request.getParameter("bookimage");

        Result res;
        try {
            biz.save(id, bookname, bookpress, pressdate, bookauthor, bookcount, bookimage);
            res = Result.success("图书保存成功", null);
        } catch (BizException e) {
            e.printStackTrace();
            res = Result.failure(e.getMessage(), null);
        } catch (Exception e) {
            e.printStackTrace();
            res = Result.failure("系统繁忙，请稍后再试，或者联系客服！", null);
        }
        Utils.writeJson(res, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
